package com.wswy.wzcx.utils;

import android.content.Context;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.wswy.wzcx.AppContext;
import com.wswy.wzcx.utils.Preference.Converter;
import io.reactivex.Maybe;
import io.reactivex.MaybeSource;
import java.util.concurrent.Callable;

import io.reactivex.Single;
import io.reactivex.SingleSource;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by wangduheng26 on 2018/1/19.
 * wangduheng26@gmail.com
 */

public class SharedPrefStorage {


    private SharedPrefStorage() {
    }

    public static void writeMessage(String key, String message) {
        PreferenceManager.getDefaultSharedPreferences(AppContext.getContext()).edit().putString(key, message).apply();
    }

    public static <T> void writeMessage(String key, T t, Converter<T> converter) {
        PreferenceManager.getDefaultSharedPreferences(AppContext.getContext()).edit().putString(key, converter.serialize(t)).apply();
    }

    public static <T> void writeMessage(String key, T t) {
        writeMessage(key, t, new SimpleListConvertAdapter<T>());
    }

    public static void writeMessage(String key, Boolean t) {
        if (t == null) return;
        PreferenceManager.getDefaultSharedPreferences(AppContext.getContext()).edit().putBoolean(key, t).apply();

    }
    public static void writeMessage(String key, Long t) {
        if (t == null) return;
        PreferenceManager.getDefaultSharedPreferences(AppContext.getContext()).edit().putLong(key, t).apply();

    }

    public static  <T> Maybe<T> readMessage(final String key, final T defaultValue, final Converter<T> converter) {
        return Maybe.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return PreferenceManager.getDefaultSharedPreferences(getContext()).getString(key, "");
            }
        }).flatMap(new Function<String, MaybeSource<? extends T>>() {
            @Override
            public MaybeSource<? extends T> apply(final String s) throws Exception {

                return Maybe.fromCallable(new Callable<T>() {
                    @Override
                    public T call() throws Exception {
                        if (TextUtils.isEmpty(s)) {
                            return defaultValue;
                        }
                        return converter.deserialize(s);
                    }
                });
            }
        });
    }

    public Single<String> readMessage(final String key, final String defaultValue) {
        return Single.fromCallable(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return PreferenceManager.getDefaultSharedPreferences(getContext()).getString(key, "");
            }
        }).subscribeOn(Schedulers.io());
    }
    public static Context getContext(){
        return AppContext.getContext();
    }
    public static Single<Boolean> readMessage(final String key, final Boolean defaultValue) {
        return Single.fromCallable(new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                return PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean(key, defaultValue);
            }
        }).subscribeOn(Schedulers.io());
    }
    public static Single<Long> readMessage(final String key, final Long defaultValue) {
        return Single.fromCallable(new Callable<Long>() {
            @Override
            public Long call() throws Exception {
                return PreferenceManager.getDefaultSharedPreferences(getContext()).getLong(key, defaultValue);
            }
        }).subscribeOn(Schedulers.io());
    }

    public static void remove(String key) {
        PreferenceManager.getDefaultSharedPreferences(getContext()).edit().remove(key).apply();

    }
}
